use std::sync::Arc;
use salvo::{Listener, Router, Server};
use salvo::affix_state::inject;
use salvo::prelude::TcpListener;
use salvo::serve_static::StaticDir;
use crate::depot::*;
use crate::hoops::*;
use crate::my_websocket::{ws_chat};
use crate::old_handler::*;
use crate::request::*;

mod hoops;
mod old_handler;
mod depot;
mod request;
mod serve_static;
mod my_websocket;

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt().init();
    let acceptor = TcpListener::new("0.0.0.0:14300").bind().await;
    Server::new(acceptor).serve(router()).await;
}
pub fn router() -> Router {
    let state = inject(Arc::new(AppState::new()));
    Router::new().hoop(state).hoop(charset_hoop)
        .push(Router::with_path("user").get(user_list).post(user_post))
        .push(Router::with_path("order_list").get(order_list))
        .push(Router::with_path("order").get(order_list_param))
        .push(Router::with_path("order/<key>").get(order_list_path_param))
        .push(Router::with_path("test_hoop").get(test_method).hoop(modify_response_body_hoop))
        .push(Router::with_path("book").get(book_list))
        .push(Router::with_path("book/add").post(book_add))
        .push(Router::with_path("animal").post(animal_add).put(animal_put))
        .push(Router::with_path("ws/chat").goal(ws_chat))
        .push(Router::with_path("<**path>").get(StaticDir::new(["dist"]).defaults("index.html").auto_list(true)))
}
